package com.xtpt.disaster.service.impl;

import com.xtpt.disaster.bean.JobDetail;
import com.xtpt.disaster.bean.TaskInfo;
import com.xtpt.disaster.bean.TaskInfoExample;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.common.PropertyConfig;
import com.xtpt.disaster.utils.ParseFileAbsolutePath;
import com.xtpt.disaster.mapper.TaskInfoMapper;
import com.xtpt.disaster.service.JobDetailService;
import com.xtpt.disaster.service.NodeInfoManageService;
import com.xtpt.disaster.service.TaskInfoManageService;
import com.xtpt.disaster.utils.IDGenerator;
import com.xtpt.disaster.utils.ServerPathUtil;
import com.xtpt.disaster.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TaskManageServiceImpl implements TaskInfoManageService {
    @Autowired
    ParseFileAbsolutePath parseFileAbsolutePath;

    @Autowired
    JobDetailService jobDetailService;

    @Autowired
    NodeInfoManageService nodeInfoManageService;

    @Autowired
    TaskInfoMapper taskInfoMapper;

    @Autowired
    ServerPathUtil serverPathUtil;

    @Autowired
    PropertyConfig config;

    @Override
    public int deleteById(long id) {
        PortalResult validation = this.validate(null, id, 2);
        if (!validation.isOK()) return 0;
        return taskInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertOneTaskInfo(TaskInfo taskInfo) {
        int count = taskInfoMapper.insertSelective(taskInfo);
        return count;
    }

    @Override
    public int insertOrUpdateTaskInfo(TaskInfo taskInfo) {
        TaskInfo task= taskInfoMapper.selectByPrimaryKey(taskInfo.getTaskId());
        if(task!=null)
        {
            return taskInfoMapper.updateByPrimaryKey(taskInfo);
        }
        return taskInfoMapper.insert(taskInfo);
    }

    /**
     * @Author jack xin
     * @Method Name insertTaskInfo
     * @Desc 插入一条任务记录，不会调用定时调度，会对taskInfo进行校验。
     * @Param [taskInfo]
     * @return com.xtpt.disaster.common.PortalResult
     * @Date 16:22 2021/12/24
    **/
    @Override
    public PortalResult insertTaskInfo(TaskInfo taskInfo) {
        PortalResult validation = this.validate(taskInfo, null, 0);
        if (!validation.isOK()) return validation;
        Date createtime = TimeUtil.dateMaker();
        Date updatetime = TimeUtil.dateMaker();
        taskInfo.setCreateTime(createtime);
        taskInfo.setUpdateTime(updatetime);
        int res = taskInfoMapper.insertSelective(taskInfo);
        if (res == 0)
            return PortalResult.error("数据库保存任务信息失败！");
        else
            return PortalResult.ok("数据库保存任务信息成功！任务设置成功！");
        }


    @Override
    public TaskInfo selectTaskInfoById(long id) {
        return taskInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateTaskInfoById(TaskInfo taskInfo) {
        Date updateTime = TimeUtil.dateMaker();
        taskInfo.setUpdateTime(updateTime);
        return taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
    }

    @Override
    public List<TaskInfo> getAllTaskInfo() {
        return taskInfoMapper.selectAll();
    }

    @Override
    public int deleteBatch(long[] ids) {
        return taskInfoMapper.deleteBatch(ids);
    }

    /**
     * taskinfo数据校验
     * @param taskInfo
     * @param action 0-insert  1-update  2-delete
     * @return
     */
    private PortalResult validate(TaskInfo taskInfo, Long taskId, Integer action){

        if(0==action || 1==action){ // 新增 or 修改
            if (StringUtils.isEmpty(taskInfo.getTaskFileType())){
                return PortalResult.error("数据类型不能为空");
            }
            if (StringUtils.isEmpty(taskInfo.getTaskType())){
                return PortalResult.error("任务类型taskType不能为空");
            }
            if (StringUtils.isEmpty(taskInfo.getTaskExectype())){
                return PortalResult.error("任务执行类型不能为空，请指定立即还是周期定时任务");
            }


            if( 0 == action){
                if(taskInfo.getTaskId()==null)
                {
                    taskInfo.setTaskId(IDGenerator.longid());
                }
                return PortalResult.ok();
            }else {
                if (StringUtils.isEmpty(taskInfo.getTaskId())){
                    return PortalResult.error("修改任务的taskId不能为空");
                }
            }
        }else {  // 删除
            if(StringUtils.isEmpty(taskId)){
                return PortalResult.error("删除任务的taskId不能为空");
            }
        }
        return  PortalResult.ok();
    }

    @Override
    public List<TaskInfo> getTaskInfoByExample(TaskInfo taskInfo) {
        TaskInfoExample taskInfoExample = new TaskInfoExample();
        TaskInfoExample.Criteria criteria = taskInfoExample.createCriteria();
        if(taskInfo != null) {
            if(!StringUtils.isEmpty(taskInfo.getTaskId())) {
                criteria.andTaskIdEqualTo(taskInfo.getTaskId());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskName())) {
                criteria.andTaskNameEqualTo(taskInfo.getTaskName());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskStarttime())) {
                criteria.andTaskStarttimeEqualTo(taskInfo.getTaskStarttime());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskEndtime())) {
                criteria.andTaskEndtimeEqualTo(taskInfo.getTaskEndtime());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskStatus())) {
                criteria.andTaskStatusEqualTo(taskInfo.getTaskStatus());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskType())) {
                criteria.andTaskTypeEqualTo(taskInfo.getTaskType());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskBackupType())) {
                criteria.andTaskBackupTypeEqualTo(taskInfo.getTaskBackupType());
            }
            if(!StringUtils.isEmpty(taskInfo.getTaskFileType())) {
                criteria.andTaskFileTypeEqualTo(taskInfo.getTaskFileType());
            }
        }
        taskInfoExample.setOrderByClause("create_time desc");
        List<TaskInfo> taskInfos = taskInfoMapper.selectByExample(taskInfoExample);

        return taskInfos;
    }


}
